
package com.cari.baju.yog;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.cari.baju.yog.database.Database;
import com.cari.baju.yog.entity.E_Toko;
import com.cari.baju.yog.helper.JSONHelper;
import com.cari.baju.yog.utils.Utils;
import com.cari.baju.yog.utils.Webservice;

public class MainActivity extends Activity implements OnClickListener {

    private Button btnCari, btnBatik, btnCasual, btnMuslim, btnBantuan, btnTentang;
    private ProgressDialog pDialog;
    private List<E_Toko> listToko;

    // category harus sama
    public static final int CATEGORY_BATIK = 1;
    public static final int CATEGORY_CASUAL = 2;
    public static final int CATEGORY_MUSLIM = 3;
    //

    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");

        title = (TextView) findViewById(R.id.txt_title);
        btnCari = (Button) findViewById(R.id.btn_cari);
        btnBatik = (Button) findViewById(R.id.btn_batik);
        btnMuslim = (Button) findViewById(R.id.btn_muslim);
        btnCasual = (Button) findViewById(R.id.btn_casual);
        btnBantuan = (Button) findViewById(R.id.btn_bantuan);
        btnTentang = (Button) findViewById(R.id.btn_tentang);

        title.setTypeface(Utils.getCustomFont(this));
        btnCari.setTypeface(Utils.getCustomFont(this));
        btnBatik.setTypeface(Utils.getCustomFont(this));
        btnMuslim.setTypeface(Utils.getCustomFont(this));
        btnCasual.setTypeface(Utils.getCustomFont(this));
        btnBantuan.setTypeface(Utils.getCustomFont(this));
        btnTentang.setTypeface(Utils.getCustomFont(this));

        btnCari.setOnClickListener(this);
        btnBatik.setOnClickListener(this);
        btnCasual.setOnClickListener(this);
        btnBantuan.setOnClickListener(this);
        btnTentang.setOnClickListener(this);
        btnMuslim.setOnClickListener(this);

        if (Utils.isConnectedToInternet(this))
            getDataFromServer(Webservice.LOCATION);

    }

    @Override
    public void onClick(View v) {

        Bundle data = new Bundle();
        switch (v.getId()) {
            case R.id.btn_cari:
                startActivity(new Intent(this, CariAllActivity.class));
                break;
            case R.id.btn_batik:
                data.putInt("cat", CATEGORY_BATIK);
                startActivity(new Intent(this, PilihCariActivity.class).putExtras(data));
                break;
            case R.id.btn_casual:
                data.putInt("cat", CATEGORY_CASUAL);
                startActivity(new Intent(this, PilihCariActivity.class).putExtras(data));
                break;
            case R.id.btn_muslim:
                data.putInt("cat", CATEGORY_MUSLIM);
                startActivity(new Intent(this, PilihCariActivity.class).putExtras(data));
                break;
            case R.id.btn_bantuan:
                startActivity(new Intent(this, BantuanActivity.class));
                break;
            case R.id.btn_tentang:
                startActivity(new Intent(this, TentangActivity.class));
                break;

        }
    }

    private void getDataFromServer(String URL) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        pDialog.dismiss();
                        Utils.TRACE("MainActivity.",
                                "onResponse : " + response.toString());
                        try {
                            if (response.getInt("error") == 0) {
                                listToko = JSONHelper.getToko(response.getJSONArray("toko"));

                                for (int i = 0; i < listToko.size(); i++) {
                                    // getData Toko
                                    E_Toko toko = Database.getInstance(MainActivity.this).getToko(
                                            listToko.get(i).getId());
                                    if (toko != null) {
                                        // jika toko ada

                                        // jika data berbeda
                                        if (toko.getTimestamp() != listToko.get(i).getTimestamp()) {
                                            Database.getInstance(MainActivity.this)
                                                    .updateToko(listToko.get(i));
                                        }
                                    } else {
                                        // jika toko tidak ada
                                        // insert
                                        Database.getInstance(MainActivity.this).insertToko(
                                                listToko.get(i));
                                    }

                                }
                            }
                        } catch (JSONException e) {
                            Utils.TRACE("MainActivity.getDataFromServer(...).new Listener() {...}",
                                    "onResponse : " + e.getMessage());
                        }
                    }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        pDialog.dismiss();
                        Utils.TRACE(
                                "MainActivity.getDataFromServer(...).new ErrorListener() {...}",
                                "onErrorResponse : " + error.getMessage());
                    }
                });
        pDialog.show();
        BaseApplication.getInstance().addToRequestQueue(request, "request");
    }
}
